namespace Stee.CAP8.MessageBus.Sagas.Order.Services
{
    using Stee.CAP8.MessageBus.Interface.Models;
    using Stee.CAP8.MessageBus.Sagas.Core.Bus;
    using Stee.CAP8.MessageBus.Sagas.Order.Sagas;
    using Stee.CAP8.MessageBus.Sagas.Payment.Dtos;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;

    public class OrderService : IOrderService
    {
        private readonly IBus _bus;

        public OrderSagaMain OrderSagaMain { get; }


        public OrderService(IBus bus, OrderSagaMain orderSagaMain)
        {
            this._bus = bus ?? throw new ArgumentNullException(nameof(bus));
            this.OrderSagaMain = orderSagaMain ?? throw new ArgumentNullException(nameof(orderSagaMain));
        }

        public async Task<bool> CreateOrder(List<ProductInfo> productInfos)
        {
            //获取用户信息 token to userinfo
            var userId = Guid.NewGuid();
            var userName = "Saga测试员";
            var mobilePhone = "13333336666";
            var orderId = Guid.NewGuid();
            var orderNumber = "TMP15987412";
            var orderInfo = new OrderInfo
            {
                UserId = userId,
                UserName = userName,
                Balance = productInfos.Sum(x => x.TotalPrice),
                MobilePhone = mobilePhone,
                OrderId = orderId,
                OrderNo = orderNumber
            };
          
            var sagaId = await this.OrderSagaMain.StartSaga(orderInfo);
            Console.WriteLine("________________________________________");
            Console.WriteLine($"Saga执行完毕，此sagaid为{sagaId}");
            Console.WriteLine("________________________________________");
            return true;
        }



      
    }





}
